//------------------------------------------------------------------------------
// X2TK Graphic Toolkit
// Copyright (C) 2006-2007 Janusz Kowalski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

#include "x2tk_VPanel.h"
#include <cassert>


namespace x2tk {

VPanel::VPanel(int x, int y, int w, int h) : Container(x,y,w,h) {
  setManageFocus(true);
}

void VPanel::align() {
  if(nChildren()>0) {
    // Vertical space (in percents of element height).
    int k=50;
    int itemW=getW();
    int itemH=100*getH()/(100*nChildren()+k*nChildren()+k);
    
    for(int i=nChildren(); i--;) {
      (*this)[i]->setX(0);
      (*this)[i]->setY(i*itemH + i*itemH*k/100);
      (*this)[i]->resize(itemW, itemH);
    }
  }
}

void VPanel::put(Widget *widget) {
  Container::add(widget);
  align();
}

void VPanel::resize(int w, int h) {
  Container::resize(w,h);
  align();
}

} // namespace x2tk
